package com.yvon.boot.common.validation.validator;

import cn.hutool.core.util.ReUtil;
import com.yvon.boot.common.validation.annotation.Phone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author : Yvon
 * @version : 1.0
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {
    /**
     * PHONE_PATTERN
     * ^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$
     */
    private static final String PHONE_PATTERN = "^[1]([3-9])[0-9]{9}$";
    /**

     * Is valid boolean.
     *
     * @param value   the value
     * @param context the context
     * @return the boolean
     * @author : Yvon / 2020-04-03
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return null == value || ReUtil.contains(PHONE_PATTERN, value);
    }
}
